<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>健康医疗</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
        <link rel="shortcut icon" href="img/favicon.ico"/>
        <!--样式-->
        <link rel="stylesheet" href="css/login.css">
        <link rel="stylesheet" href="element-ui/index.css">
        <style>
            .login-form .el-input input, .el-button {
                background-color: rgba(0, 0, 0, 0.5);
            }

            .login-form .el-input input {
                color: white;
                font-weight: bolder;
            }
        </style>
    </head>
    <body>
        <div id="particles-js">
            <div id="app" class="login" v-loading="loading">
                <div class="login-top">
                    健康医疗管理系统
                </div>
                <section>
                    <el-form class="login-form" ref="loginForm" :model="user" :rules="rules" label-width="80px">
                        <el-form-item prop="username">
                            <el-input v-model="user.username" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input type="password" v-model="user.password" placeholder="请输入密码"
                                      prefix-icon="el-icon-lock" @keyup.enter.native="login()"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="login">登录</el-button>
                        </el-form-item>
                    </el-form>
                </section>
            </div>
        </div>

        <!--js库-->
        <script src="js/particles.min.js"></script>
        <script src="js/login.js"></script>
        <script src="js/axios.min.js"></script>
        <script src="js/vue.js"></script>
        <script src="element-ui/index.js"></script>
        <script>
            var vue = new Vue({
                el: "#app",
                data: {
                    loading: false,
                    user: {
                        username: "",
                        password: ""
                    },
                    rules: {
                        username: [
                            {required: true, message: '请输入账号', trigger: 'blur'}
                        ],
                        password: [
                            {required: true, message: '请输入密码', trigger: 'blur'}
                        ]
                    }
                },
                methods: {
                    login() {
                        this.$refs["loginForm"].validate((valid) => {
                            if (valid) {
                                //打开遮罩
                                this.loading = true;

                                //发送登录请求
                                var url = "login?username=" + this.user.username
                                    + "&password=" + this.user.password;
                                axios.post(url)
                                    .then(res => {
                                        if (res.data.flag) {
                                            //存储用户名到本地
                                            localStorage.loginUsername = res.data.data;
                                            //跳转到首页
                                            location.href = "pages/main.html";
                                        } else {
                                            this.$message.warning(res.data.msg);
                                        }

                                        //关闭遮罩
                                        this.loading = false;
                                    })
                                    .catch(error => {
                                        this.$message.error("请求出错：" + error);
                                        //关闭遮罩
                                        this.loading = false;
                                    });
                            }
                        });
                    }
                }
            });
        </script>
    </body>
</html>